package com.shkco.org.service.impl;

import com.shkco.basic.service.impl.BaseServiceImpl;
import com.shkco.basic.util.Md5Utils;
import com.shkco.basic.util.PageList;
import com.shkco.basic.util.StrUtils;
import com.shkco.org.domain.Department;
import com.shkco.org.domain.Employee;
import com.shkco.org.mapper.DepartmentMapper;
import com.shkco.org.mapper.EmployeeMapper;
import com.shkco.org.query.EmployeeQuery;
import com.shkco.org.service.IEmployeeService;
import com.shkco.user.domain.Logininfo;
import com.shkco.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void add(Employee employee) {
        // 使用工具类生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 使用工具类加密加盐
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        // 给user设置盐值
        employee.setSalt(salt);
        // 设置密码
        employee.setPassword(md5Pwd);

        // 保存logininfo
        Logininfo logininfo = new Logininfo();
        // 使用工具类拷贝
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);
        // 保存employee
        employee.setLogininfo_id(logininfo.getId());

        super.add(employee);
        if (employee.getRoleId() != null) {
            employeeMapper.addEmployeeRole(employee);
        }
    }

    @Override
    public void del(Long id) {
        Employee employee = employeeMapper.findById(id);
        logininfoMapper.del(employee.getLogininfo_id());

        employeeMapper.delEmployeeRoleByEmpId(id);
        super.del(id);
    }

    @Override
    public void update(Employee employee) {
        // 使用工具类生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 使用工具类加密加盐
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        // 给user设置盐值
        employee.setSalt(salt);
        // 设置密码
        employee.setPassword(md5Pwd);

        employeeMapper.delEmployeeRoleByEmpId(employee.getId());
        if (employee.getRoleId() != null) {
            employeeMapper.addEmployeeRole(employee);
        }
        super.update(employee);

        // 获取登录信息id
        Long logininfoId = employeeMapper.findById(employee.getId()).getLogininfo_id();
        // 获取logininfo对象
        Logininfo logininfo = logininfoMapper.findById(logininfoId);
        // 将原有的数据覆盖
        BeanUtils.copyProperties(employee,logininfo);
        logininfoMapper.update(logininfo);
    }

    @Override
    public List<Employee> findByShopId(Long shopId) {
        return employeeMapper.loadByShopId(shopId);
    }
}
